﻿$(document).ready(function () {

    ko.bindingHandlers.date = {
        update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
            var value = valueAccessor();
            var formatString = allBindingsAccessor().formatString;
            var date;
            if (typeof value == 'function') {
                date = moment(value());
            }
            else {
                date = moment(value);
            }

            if (date == null) {
                $(element).text('');
                $(element).val(null);
            }
            else {
                if (formatString == null) {
                    $(element).text(date.format('MM/DD/YY'));
                    $(element).val(date.format('MM/DD/YY'));
                }
                else {
                    $(element).text(date.format(formatString));
                    $(element).val(date.format(formatString));
                }
            }
        }
    };

    ko.observableArray.fn.distinct = function (prop) {
        var target = this;
        target.index = {};
        target.index[prop] = ko.observable({});

        ko.computed(function () {
            //rebuild index
            var propIndex = {};

            ko.utils.arrayForEach(target(), function (item) {
                var key = ko.utils.unwrapObservable(item[prop]);
                if (key) {
                    propIndex[key] = propIndex[key] || [];
                    propIndex[key].push(item);
                }
            });

            target.index[prop](propIndex);
        });

        return target;
    };

    var loadBookings = function (vm, status, startIndex) {

        var bookingData = bookingImplement.getBookings(
            status,
            startIndex,
            $('#keyword').val(),
            $('#fromDate').val(),
            $('#toDate').val());

        var bindingList = [];

        $.each(bookingData.List, function () {
            bindingList.push(new bookingModel(this));
        });

        if (reload) {
            // Set
            var array = ko.observableArray(bindingList).distinct('groupBy');

            vm.rowCount(bookingData.Count);
            vm.bookingVM().allSelected(false);
            vm.bookingVM().bookingGroups(bookingData.BookingGrouped);
            vm.bookingVM().bookings.removeAll();

            $.each(array(), function () {
                vm.bookingVM().bookings.push(this);
            });
        }
        else {
            // Init
            vm.prototype.bookingVM = ko.observable(new bookingVM(bookingData.PageSize));


            // Here edit
            self.commentVM().allItemCount(o.AllItemCount);
            self.commentVM().allSelected(false);
            self.commentVM().choices = ko.observableArray(o.Choices);
            self.commentVM().comments = ko.observableArray(list).distinct('groupBy');

            if (self.status() == 'draft' || self.status() === 'pending') {
                self.trackbackVM = ko.observable(new CommentViewModel(self, o1.PageSize));

                self.trackbackVM().allItemCount(o1.AllItemCount);
                self.trackbackVM().allSelected(false);
                self.trackbackVM().choices = ko.observableArray(o1.Choices);
                self.trackbackVM().comments = ko.observableArray(list1).distinct('groupBy');
            }

            reload = true;
        }
    }

    var bookingVM = function (pageSize) {
        var self = this;

        self.rowCount = ko.observable(0);
        self.allSelected = ko.observable(false);
        self.startIndex = ko.observable(1);

        self.previousPage = function () {
            self.startIndex(self.startIndex() - pageSize);
            loadBookings();
        };

        self.nextPage = function () {
            self.startIndex(self.startIndex() + pageSize);
            loadBookings();
        };

        self.currentPage = ko.computed(function () {
            return self.startIndex() + " - " + Math.min(self.startIndex() + pageSize - 1, self.rowCount());
        });

        self.canNextPage = ko.computed(function () {
            return self.startIndex() + pageSize - 1 < self.rowCount();
        });

        self.selectAll = function () {
            var all = self.allSelected();
            ko.utils.arrayForEach(self.booking(), function (item) {
                item.selected(!all);
            });
            return true;
        };
    });
});